package com.esp.soccertutor.ui.xmlparser;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.esp.soccertutor.ui.bean.BeanProductDetails;
import com.esp.soccertutor.util.Const;
import com.esp.soccertutor.util.Log;
import com.esp.soccertutor.util.Utils;

public class XMLParser_SCProductDetails {
	
	public String TAG = XMLParser_SCProductDetails.class.getCanonicalName();
	public static int STATUS_CODE;
	private String strServerXML;
	private static Document docShowcaseDetails;
	public int RESPONSE_CODE = -1;
	public String MESSAGE;
	public ArrayList<BeanProductDetails> mShowcaseProDetails;
	
	
	public XMLParser_SCProductDetails(String wsURL){
		

		try
		{
			XMLDocument.getHttpGetXML(wsURL);
			strServerXML = Utils.readTxt(Const.CONTEXT,"ProductInfo.txt");

			if(XMLDocument.HTTP_XML_STATUS == 200)
			{
				Log.print(TAG,strServerXML);
				docShowcaseDetails = XMLDocument.XMLfromString(strServerXML.replaceAll("&","&amp;").trim());
				STATUS_CODE = 200;
				
			}
			else if(XMLDocument.HTTP_XML_STATUS == 401)
			{
				// XML not validating 
				STATUS_CODE = 401;
			}
			else if(XMLDocument.HTTP_XML_STATUS == 500)
			{
				// 	internal error occurred								
				STATUS_CODE = 500;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			STATUS_CODE = 500;
		}
		
	}
	
	public static Document GetDocument(){
		
		if(docShowcaseDetails != null){
			return docShowcaseDetails;
		}else{
			return null;
		}
	}
	public ArrayList<BeanProductDetails> getProductDetails(Document docCountry){
		try
		{
			if(docCountry != null)
				return docBuild(docCountry);
			else
				return null;
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	private ArrayList<BeanProductDetails> docBuild(Document doc) throws Exception{
		
		if(doc != null)
		{
			Element elem$root = doc.getDocumentElement();
			NodeList nl$item = elem$root.getElementsByTagName("code");
			Element  line  = (Element)nl$item.item(0);
			RESPONSE_CODE = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line, "code"));
			
			NodeList nodeMessage = elem$root.getElementsByTagName("message");
			line  = (Element)nodeMessage.item(0);
			MESSAGE = XMLDocument.getCharacterDataFromElement(line, "message");
			
			if(RESPONSE_CODE == 1)
			{
				mShowcaseProDetails = new ArrayList<BeanProductDetails>();
				BeanProductDetails mProductDetails = new BeanProductDetails();
				
				NodeList nodeID = elem$root.getElementsByTagName("id");
				line = (Element) nodeID.item(0);
				mProductDetails.setId(XMLDocument.getCharacterDataFromElement(line, "id"));
				
				NodeList nodeTitle = elem$root.getElementsByTagName("title");
				line  = (Element)nodeTitle.item(0);
				mProductDetails.setTitle(XMLDocument.getCharacterDataFromElement(line, "title"));
				
				
				NodeList nodeImageURL = elem$root.getElementsByTagName("imageURL");
				line  = (Element)nodeImageURL.item(0);
				mProductDetails.setImageURL(XMLDocument.getCharacterDataFromElement(line, "imageURL"));
				
				
				NodeList nodeNormalPrice = elem$root.getElementsByTagName("normalprice");
				line  = (Element)nodeNormalPrice.item(0);
				mProductDetails.setNormalprice(XMLDocument.getCharacterDataFromElement(line, "normalprice"));
				
				
				NodeList nodeIntroPrice = elem$root.getElementsByTagName("introductoryprice");
				line  = (Element)nodeIntroPrice.item(0);
				mProductDetails.setIntroductoryprice(XMLDocument.getCharacterDataFromElement(line, "introductoryprice"));
				
				NodeList nodeYouSave = elem$root.getElementsByTagName("yousave");
				line  = (Element)nodeYouSave.item(0);
				mProductDetails.setYousave(XMLDocument.getCharacterDataFromElement(line, "yousave"));
				
				
				NodeList nodeNormalPriceGBP = elem$root.getElementsByTagName("normalpriceGBP");
				line  = (Element)nodeNormalPriceGBP.item(0);
				mProductDetails.setNormalpriceGBP(XMLDocument.getCharacterDataFromElement(line, "normalpriceGBP"));
				
				
				
				NodeList nodeIntroPriceGBP = elem$root.getElementsByTagName("introductorypriceGBP");
				line  = (Element)nodeIntroPriceGBP.item(0);
				mProductDetails.setIntroductorypriceGBP(XMLDocument.getCharacterDataFromElement(line, "introductorypriceGBP"));
				
				
				NodeList nodeYouSaveGBP = elem$root.getElementsByTagName("yousaveGBP");
				line  = (Element)nodeYouSaveGBP.item(0);
				mProductDetails.setYousaveGBP(XMLDocument.getCharacterDataFromElement(line, "yousaveGBP"));
				
				
				NodeList nodeStoreURL = elem$root.getElementsByTagName("storeURL");
				line  = (Element)nodeStoreURL.item(0);
				mProductDetails.setStoreURL(XMLDocument.getCharacterDataFromElement(line, "storeURL"));
				
				NodeList nodePreviewFile = elem$root.getElementsByTagName("previewFile");
				line  = (Element)nodePreviewFile.item(0);
				mProductDetails.setPreviewFile(XMLDocument.getCharacterDataFromElement(line, "previewFile"));
				
				NodeList nodeDes = elem$root.getElementsByTagName("description");
				line  = (Element)nodeDes.item(0);
				mProductDetails.setDescription(XMLDocument.getCharacterDataFromElement(line, "description"));
				
				
				mShowcaseProDetails.add(mProductDetails);
				
					
			}
			
		}
		return mShowcaseProDetails;
	}
	
	
	
}
